# Hundred Day Action System Startup Script
# PowerShell Version

param(
    [switch]$NewWindows  # Launch in separate windows
)

Write-Host "==========================================" -ForegroundColor Green
Write-Host "  Hundred Day Action System Launcher" -ForegroundColor Green  
Write-Host "==========================================" -ForegroundColor Green
Write-Host ""

# Check Node.js installation
Write-Host "Checking Node.js..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version 2>$null
    Write-Host "Node.js installed: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "ERROR: Node.js not found. Please install Node.js first" -ForegroundColor Red
    Write-Host "Download: https://nodejs.org/" -ForegroundColor White
    Read-Host "Press any key to exit"
    exit 1
}

# Check directories
if (-not (Test-Path "backend")) {
    Write-Host "ERROR: backend directory not found" -ForegroundColor Red
    Read-Host "Press any key to exit"
    exit 1
}

if (-not (Test-Path "frontend")) {
    Write-Host "ERROR: frontend directory not found" -ForegroundColor Red
    Read-Host "Press any key to exit"
    exit 1
}

Write-Host "Project directories found" -ForegroundColor Green
Write-Host ""

# Set environment variables
Write-Host "Setting environment variables..." -ForegroundColor Yellow
$env:DB_HOST = "localhost"
$env:DB_PORT = "3306"
$env:DB_USER = "root"
$env:DB_PASSWORD = "newhand"
$env:DB_NAME = "hunaction"
$env:PORT = "3001"
$env:NODE_ENV = "development"

Write-Host "Database config: $($env:DB_HOST):$($env:DB_PORT)" -ForegroundColor Green
Write-Host "Database name: $($env:DB_NAME)" -ForegroundColor Green
Write-Host "Server port: $($env:PORT)" -ForegroundColor Green
Write-Host ""

# Install dependencies if needed
Write-Host "Checking dependencies..." -ForegroundColor Yellow

# Backend dependencies
Push-Location "backend"
if (-not (Test-Path "node_modules")) {
    Write-Host "Installing backend dependencies..." -ForegroundColor Yellow
    npm install
}
Pop-Location

# Frontend dependencies  
Push-Location "frontend"
if (-not (Test-Path "node_modules")) {
    Write-Host "Installing frontend dependencies..." -ForegroundColor Yellow
    npm install
}
Pop-Location

Write-Host "Dependencies ready" -ForegroundColor Green
Write-Host ""

$currentPath = Get-Location

if ($NewWindows) {
    # Launch in separate windows
    Write-Host "Starting services in separate windows..." -ForegroundColor Cyan
    
    # Start Backend
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$currentPath\backend'; `$env:DB_HOST='localhost'; `$env:DB_PORT='3306'; `$env:DB_USER='root'; `$env:DB_PASSWORD='newhand'; `$env:DB_NAME='hunaction'; `$env:PORT='3001'; `$env:NODE_ENV='development'; Write-Host 'Starting Backend Server...' -ForegroundColor Blue; node app.js"
    
    Start-Sleep -Seconds 2
    
    # Start Frontend
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$currentPath\frontend'; Write-Host 'Starting Frontend Server...' -ForegroundColor Blue; npm start"
    
    Write-Host ""
    Write-Host "Services started in separate windows!" -ForegroundColor Green
    Write-Host "Backend: http://localhost:3001" -ForegroundColor White
    Write-Host "Frontend: http://localhost:8080" -ForegroundColor White
    Write-Host "Admin Panel: http://localhost:3001/admin/login.html" -ForegroundColor White
    
} else {
    # Launch minimized
    Write-Host "Starting services..." -ForegroundColor Cyan
    
    # Start Backend process
    Write-Host "Starting Backend server..." -ForegroundColor Blue
    $backendProcess = Start-Process powershell -ArgumentList "-WindowStyle", "Minimized", "-Command", "cd '$currentPath\backend'; `$env:DB_HOST='localhost'; `$env:DB_PORT='3306'; `$env:DB_USER='root'; `$env:DB_PASSWORD='newhand'; `$env:DB_NAME='hunaction'; `$env:PORT='3001'; `$env:NODE_ENV='development'; node app.js" -PassThru
    
    Start-Sleep -Seconds 3
    
    # Start Frontend process
    Write-Host "Starting Frontend server..." -ForegroundColor Blue
    $frontendProcess = Start-Process powershell -ArgumentList "-WindowStyle", "Minimized", "-Command", "cd '$currentPath\frontend'; npm start" -PassThru
    
    Start-Sleep -Seconds 2
    
    Write-Host ""
    Write-Host "System started successfully!" -ForegroundColor Green
    Write-Host "==========================================" -ForegroundColor Green
    Write-Host "Backend API: http://localhost:3001" -ForegroundColor White
    Write-Host "Frontend: http://localhost:8080" -ForegroundColor White
    Write-Host "Admin Panel: http://localhost:3001/admin/login.html" -ForegroundColor White
    Write-Host "Submit Page: http://localhost:8080/submit.html" -ForegroundColor White
    Write-Host "==========================================" -ForegroundColor Green
    Write-Host ""
    Write-Host "Press Ctrl+C to stop all services" -ForegroundColor Yellow
    Write-Host "Process IDs - Backend: $($backendProcess.Id), Frontend: $($frontendProcess.Id)" -ForegroundColor Gray
    
    # Wait for user interrupt
    try {
        Write-Host "Services running... (Press Ctrl+C to stop)" -ForegroundColor Yellow
        while ($true) {
            Start-Sleep -Seconds 5
            
            # Check if processes are still running
            $backendRunning = Get-Process -Id $backendProcess.Id -ErrorAction SilentlyContinue
            $frontendRunning = Get-Process -Id $frontendProcess.Id -ErrorAction SilentlyContinue
            
            if (-not $backendRunning) {
                Write-Host "WARNING: Backend process stopped" -ForegroundColor Yellow
            }
            if (-not $frontendRunning) {
                Write-Host "WARNING: Frontend process stopped" -ForegroundColor Yellow
            }
            
            if (-not $backendRunning -and -not $frontendRunning) {
                Write-Host "All services stopped" -ForegroundColor Red
                break
            }
        }
    } catch [System.Management.Automation.CommandInterruptedException] {
        Write-Host ""
        Write-Host "Stopping services..." -ForegroundColor Yellow
        
        # Stop processes
        if (Get-Process -Id $backendProcess.Id -ErrorAction SilentlyContinue) {
            Stop-Process -Id $backendProcess.Id -Force
            Write-Host "Backend service stopped" -ForegroundColor Green
        }
        if (Get-Process -Id $frontendProcess.Id -ErrorAction SilentlyContinue) {
            Stop-Process -Id $frontendProcess.Id -Force
            Write-Host "Frontend service stopped" -ForegroundColor Green
        }
    }
}

Write-Host ""
Write-Host "==========================================" -ForegroundColor Green
Write-Host "  Thank you for using our system!" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green 